package at.codebase.db.entities;

import java.util.ArrayList;

import com.google.appengine.api.search.GeoPoint;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Ref;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.Load;

/**
 * @author Daniel Marth <danielmarth@gmx.at>
 * @author Lukas Pfeifhofer <lukas.pfeifhofer@devlabs.pro>
 */
@Entity
public class EItem {

	@Id
	private Long itemId;
	@Load
	@Index
	Ref<EUser> seller;

	private String title;
	private String description;
	private boolean active;
	private boolean deleted;
	private double price;

	@Index
	private long timeCreate;
	private long timeExpiration;

	private boolean useLocation;
	private double latitude;
	private double longitude;

	private ArrayList<Key<EImage>> imageKeys = new ArrayList<Key<EImage>>();

	/*
	 * Objectify
	 */
	public static Key<EItem> key(Long itemId) {
		return Key.create(EItem.class, itemId.longValue());
	}

	public static Key<EItem> key(String raw) {
		return Key.create(raw);
	}

	public static Ref<EItem> ref(EItem item) {
		Key<EItem> key = key(item.getId());
		return Ref.create(key, item);
	}

	/*
	 * Util
	 */
	public boolean checkSeller(Long userId) {
		return getSeller().key().equals(EUser.key(userId));
	}

	/*
	 * Constructors
	 */
	public EItem() {

	}

	public EItem(Ref<EUser> seller, String title, String description,
			boolean active, boolean deleted, double price, long timeCreate,
			long timeExpiration) {
		this(seller, title, description, active, deleted, price, timeCreate,
				timeExpiration, 0.0, 0.0);
		this.useLocation = false;
	}

	public EItem(Ref<EUser> seller, String title, String description,
			boolean active, boolean deleted, double price, long timeCreate,
			long timeExpiration, double latitude, double longitude) {
		this.seller = seller;
		this.title = title;
		this.description = description;
		this.active = active;
		this.deleted = deleted;
		this.price = price;
		this.timeCreate = timeCreate;
		this.timeExpiration = timeExpiration;
		this.latitude = latitude;
		this.longitude = longitude;
		this.useLocation = true;
	}
	
	public static EItem createImportItem(Ref<EUser> seller, String itemTitle, String itemDescription, double itemPrice) {
		EItem item = createItem(seller, itemTitle, itemDescription, itemPrice, System.currentTimeMillis(),
				System.currentTimeMillis() + 1209600000);
		return item;
	}

	public static EItem createItem(Ref<EUser> seller, String itemTitle,
			String itemDescription, double itemPrice, long timeCreate,
			long timeExpiration) {
		return new EItem(seller, itemTitle, itemDescription, true, false,
				itemPrice, timeCreate, timeExpiration);
	}

	public static EItem createItem(Ref<EUser> seller, String itemTitle,
			String itemDescription, double itemPrice, long timeCreate,
			long timeExpiration, double latitude, double longitude) {
		return new EItem(seller, itemTitle, itemDescription, true, false,
				itemPrice, timeCreate, timeExpiration, latitude, longitude);
	}

	public Long getId() {
		return itemId;
	}

	public void setId(Long itemId) {
		this.itemId = itemId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public boolean isDeleted() {
		return deleted;
	}

	public void setDeleted(boolean deleted) {
		this.deleted = deleted;
	}

	public double getPrice() {
		return price;
	}

	public void setPrice(double price) {
		this.price = price;
	}

	public long getTimeCreate() {
		return timeCreate;
	}

	public void setTimeCreate(long timeCreate) {
		this.timeCreate = timeCreate;
	}

	public long getTimeExpiration() {
		return timeExpiration;
	}

	public void setTimeExpiration(long timeExpiration) {
		this.timeExpiration = timeExpiration;
	}

	public boolean isExpired() {
		return (System.currentTimeMillis() > timeExpiration);
	}

	public Ref<EUser> getSeller() {
		return seller;
	}

	public void setSeller(Ref<EUser> seller) {
		this.seller = seller;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public double getLatitude() {
		return latitude;
	}

	public void setLocation(double latitude, double longitude) {
		this.latitude = latitude;
		this.longitude = longitude;
		this.useLocation = true;
	}

	public double getLongitude() {
		return longitude;
	}

	public ArrayList<Key<EImage>> getImageKeys() {
		return imageKeys;
	}

	public void setImageKeys(ArrayList<Key<EImage>> imageKeys) {
		this.imageKeys = imageKeys;
	}

	@Override
	public String toString() {
		return "EItem [itemId=" + itemId + ", seller=" + seller + ", title="
				+ title + ", description=" + description + ", active=" + active
				+ ", deleted=" + deleted + ", price=" + price + ", timeCreate="
				+ timeCreate + ", timeExpiration=" + timeExpiration
				+ ", latitude=" + latitude + ", longitude=" + longitude
				+ ", imageKeys=" + imageKeys + "]";
	}

	public GeoPoint getLocation() {
		if (!useLocation) {
			return null;
		}
		return new GeoPoint(latitude, longitude);
	}

	public void setLocation(GeoPoint location) {
		if (location == null) {
			latitude = longitude = 0.0;
			useLocation = false;
		} else {
			latitude = location.getLatitude();
			longitude = location.getLongitude();
			useLocation = true;
		}
	}

}
